package com.sniff.atp.service.order;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.ClientLoginDTO;
import com.sniff.atp.dto.ContentDTO;
import com.sniff.atp.enums.api.OrderAPI;
import com.sniff.atp.utils.Headers;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CombinationOrderService {
    @Autowired
    private OrderService orderService;

    public String productPage(ClientLoginDTO clientLoginDTO, String keywordSku) {
        JSONObject params = JsonUtil.readJson("order/combination/CombinationProductPage", new ContentDTO("keywordSku", keywordSku));
        return new HttpClientUtil(Headers.setClientHeader(clientLoginDTO))
                .doPostJson(OrderAPI.COM_PAGE.getApi(), params);
    }

    public JSONObject getCombinationProduct(ClientLoginDTO clientLoginDTO, String combinationProductSku) {
        String productDetail = productPage(clientLoginDTO, combinationProductSku);
        List<String> shopProductSkus = JsonUtil.extractMulti(productDetail, "shopProductSku");
        List<Integer> minOrderQuantity = JsonUtil.extractMulti(productDetail, "minOrderQuantity");
        JSONArray combinationProductItemAddCartList = new JSONArray();
        shopProductSkus.forEach(item -> {
            if (item != null) {
                JSONObject combinationProductItemAddCart = JsonUtil.readJson("order/combination/CombinationProductItemAddCart", new ContentDTO("shopProductSku", item));
                combinationProductItemAddCartList.add(combinationProductItemAddCart);
            }
        });
        JSONObject combinationProduct = JsonUtil.readJson("order/combination/CombinationProductSku", new ContentDTO("combinationProductItemAddCartList", combinationProductItemAddCartList.toString()));
        combinationProduct.put("combinationProductSku", combinationProductSku);
        JSONObject product = new JSONObject();
        product.put("combinationProduct", combinationProduct);
        product.put("minOrderQuantity", minOrderQuantity.stream().max(Integer::compareTo).orElse(1));
        return product;
    }

    public List<String> combinationMultiAddCart(ClientLoginDTO clientLoginDTO, List<String> combinationProductSkus, Integer num) {
        List<String> cartIds = new ArrayList<>();
        combinationProductSkus.forEach(combinationProductSku -> cartIds.add(combinationAddCart(clientLoginDTO, combinationProductSku, num)));
        return cartIds;
    }

    public String combinationAddCart(ClientLoginDTO clientLoginDTO, String combinationProductSku, Integer num) {
        JSONObject product = getCombinationProduct(clientLoginDTO, combinationProductSku);
        Integer minOrderQuantity = product.getInteger("minOrderQuantity");
        JSONArray combinationProductSkuList = JSONArray.of(product.getJSONObject("combinationProduct"));
        JSONObject combinationGoods = JsonUtil.readJson("order/combination/CombinationProductAddCart");
        combinationGoods.put("combinationProductSkuList", combinationProductSkuList);
        String addCart = orderService.clientAddCart(clientLoginDTO, combinationGoods);
        String cartId = JsonUtil.extractString(addCart, "data." + combinationProductSku);
        if (num != null) {
            orderService.clientCartQuantity(clientLoginDTO, cartId, num);
        } else {
            if (minOrderQuantity > 1) {
                orderService.clientCartQuantity(clientLoginDTO, cartId, minOrderQuantity);
            }
        }
        return cartId;
    }
}
